package com.yellowpage.bigdata.action;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.opensymphony.xwork2.ActionContext;
import com.yellowpage.bigdata.data.NspFatallogImeiWeekregionSum;
import com.yellowpage.bigdata.service.NspFatallogImeiWeekregionSumService;
import com.yellowpage.ca.data.User;
import com.yellowpage.ca.service.RegionService;
import com.yellowpage.white.common.Configuration;
import com.yellowpage.white.struts.BaseAction;

public class NspFatallogImeiWeekregionSumAction extends BaseAction {

	public String RESULT_QUERY = "query";
	public String RESULT_ADD = "add";
	public String RESULT_EDIT = "edit";
	private RegionService regionService;
	private Configuration configuration;

	public void setConfiguration(Configuration configuration) {
		this.configuration = configuration;
	}

	private List<NspFatallogImeiWeekregionSum> nspFatallogImeiWeekregionSumList = new ArrayList<NspFatallogImeiWeekregionSum>();

	private NspFatallogImeiWeekregionSum nspFatallogImeiWeekregionSum;
	private NspFatallogImeiWeekregionSumService nspFatallogImeiWeekregionSumService;

	private String selectedId;
	private String[] selectedIds;
	private String message;

	private String query_fatallogImeiWeekregionSumId;
	private String query_regionCode;
	private String query_logWeek;
	private String query_createTime;
	private String query_note;
	private String query_createUser;
	private String query_requestType;
	private String query_imeiWeekCount;
	private String query_imeiNewCount;
	private String query_imeiAllCount;
	private String query_logWeek_end;
	
	private List<Map> resultList = new ArrayList<Map>();
	private Map<String,String> channelMap = new HashMap<String,String>();
	private Map<String,String> requestTypeList;
	private String query_channel;
	private Map requestTypeMap = new HashMap();
	private String flag;
	private String channelCode;
	private String regionName;
	private Map<String, String> regionMap;
	public static boolean isType = false;
	public RegionService getRegionService() {
		return regionService;
	}

	public void setRegionService(RegionService regionService) {
		this.regionService = regionService;
	}
	 public String getRegionSql(){
   	  Map session = ActionContext.getContext().getSession();
   	  User user = (User) session.get("userObject");
   	  String function_code= "OMC_ABILITYCALL_LOG_COUNT_WEEK_COUNT";
   	 String regionsql= regionService.getRegionclauseByUserFunction(user.getUserId(), function_code, "REGION_CODE");
   	 return regionsql;
   }
	public void prepare() {
		super.prepare();
//		regionMap = nspFatallogImeiWeekregionSumService.getRegionMap();
//		channelMap = nspFatallogImeiWeekregionSumService.getChannelMap();
		if (this.selectedId != null && !this.selectedId.equals("")) {
			this.nspFatallogImeiWeekregionSum = nspFatallogImeiWeekregionSumService
					.getNspFatallogImeiWeekregionSum(Long.parseLong(selectedId));
		}
	}

	public String queryByPage() {
		initPageRender();
       
       
		Map queryMap = new HashMap();
		//渠道 and请求类型
		channelMap = nspFatallogImeiWeekregionSumService.getChannelMap();
		String str = "(";
		if(!channelCode.equals("") && query_requestType.equals("")){
			String val[] = channelCode.split(";");
			for(String ss : val){
				for(String key : channelMap.keySet()){
					if(ss.equals(channelMap.get(key))){
						str += "'" + key + "',";
					}
				}
			}
			isType=true;
			str= str.substring(0, str.length() - 1)+")";
			queryMap.put("requestType", str);
		}else if(!query_requestType.equals("")){
				requestTypeList = nspFatallogImeiWeekregionSumService
						.getrequestTypeMap();
				String[] val = query_requestType.split(";");
				for (String str1 : val) {
					for (String ss : requestTypeList.keySet()) {
						if (requestTypeList.get(ss).equals(str1)) {
							str += "'" + ss + "',";
							break;
						}
					}
				}
				isType=false;
				str= str.substring(0, str.length() - 1)+")";
			queryMap.put("requestType", str);
		}else{
				isType=false;
			queryMap.put("requestType", query_requestType);
		}
		//地区
		if(!regionName.equals("")){
			regionMap = nspFatallogImeiWeekregionSumService.getRegionMap(getRegionSql());
			String[] val = regionName.split(";");
			str = "(";
			for(String ss : val){
				for(String key : regionMap.keySet()){
					if(regionMap.get(key).contains(ss)){
						str += "'" + key + "',";
						break;
					}
				}
			}
			str= str.substring(0, str.length() - 1)+")";
			queryMap.put("regionCode", str);
		}else{
			queryMap.put("regionCode", regionName);
		}
		queryMap.put("logWeek", query_logWeek);
		queryMap.put("logWeek_end", query_logWeek_end);
		resultList = nspFatallogImeiWeekregionSumService
				.queryNspFatallogImeiWeekregionSumByPage(queryMap, pageRender);

		setPageRenderToRequest();

		return RESULT_QUERY;
	}

	public String queryChannel(){
		initPageRender();
		if(flag.equals("1")){
			requestTypeMap = nspFatallogImeiWeekregionSumService.getrequestTypeMap();
		}else if(flag.equals("0")){
			channelMap = nspFatallogImeiWeekregionSumService.getChannelMap();
		}else{
			regionMap = nspFatallogImeiWeekregionSumService.getRegionMap(getRegionSql());
		}
		setPageRenderToRequest();
		return flag.equals("1") ? "toRequest":(flag.equals("0") ? "toChannel" : "toRegion");
	}
	public String toAdd() {
		return RESULT_ADD;
	}

	public String save() {

		nspFatallogImeiWeekregionSumService
				.saveNspFatallogImeiWeekregionSum(nspFatallogImeiWeekregionSum);

		message = "新增成功!";
		return RESULT_ADD;
	}

	public void validateSave() {
		if (nspFatallogImeiWeekregionSum.getRegionCode().length() == 0) {
			this.addActionError("** RegionCode 不能为空 !");
		}
		if (nspFatallogImeiWeekregionSum.getLogWeek().length() == 0) {
			this.addActionError("** LogWeek 不能为空 !");
		}
		if (nspFatallogImeiWeekregionSum.getNote().length() == 0) {
			this.addActionError("** Note 不能为空 !");
		}
		if (nspFatallogImeiWeekregionSum.getRequestType().length() == 0) {
			this.addActionError("** RequestType 不能为空 !");
		}

		this.setValidatorResult("/bigdata/NspFatallogImeiWeekregionSumAdd.jsp");
	}

	public String toEdit() {
		Long id = new Long(selectedId);

		nspFatallogImeiWeekregionSum = nspFatallogImeiWeekregionSumService
				.getNspFatallogImeiWeekregionSum(id);

		return RESULT_EDIT;
	}

	public String update() {

		nspFatallogImeiWeekregionSumService
				.updateNspFatallogImeiWeekregionSum(nspFatallogImeiWeekregionSum);

		message = "修改成功!";

		return RESULT_EDIT;
	}

	public void validateUpdate() {
		if (nspFatallogImeiWeekregionSum.getRegionCode().length() == 0) {
			this.addActionError("** RegionCode 不能为空 !");
		}
		if (nspFatallogImeiWeekregionSum.getLogWeek().length() == 0) {
			this.addActionError("** LogWeek 不能为空 !");
		}
		if (nspFatallogImeiWeekregionSum.getNote().length() == 0) {
			this.addActionError("** Note 不能为空 !");
		}
		if (nspFatallogImeiWeekregionSum.getRequestType().length() == 0) {
			this.addActionError("** RequestType 不能为空 !");
		}
		this.setValidatorResult("/bigdata/NspFatallogImeiWeekregionSumUpdate.jsp");
	}

	public String delete() {
		List ids = new ArrayList();

		for (String id : selectedIds) {
			ids.add(Long.valueOf(id));
		}

		try {
			this.nspFatallogImeiWeekregionSumService.delete(ids);
		} catch (Exception e) {
			System.out.println("异常....");
			this.addActionError("** 可能含有子记录约束冲突删除失败 !");
		}

		return queryByPage();
	}

	/**
	 * getters and setters
	 * 
	 * @return
	 */
	public void setNspFatallogImeiWeekregionSum(
			NspFatallogImeiWeekregionSum nspFatallogImeiWeekregionSum) {
		this.nspFatallogImeiWeekregionSum = nspFatallogImeiWeekregionSum;
	}

	public NspFatallogImeiWeekregionSum getNspFatallogImeiWeekregionSum() {
		return nspFatallogImeiWeekregionSum;
	}

	public void setNspFatallogImeiWeekregionSumService(
			NspFatallogImeiWeekregionSumService nspFatallogImeiWeekregionSumService) {
		this.nspFatallogImeiWeekregionSumService = nspFatallogImeiWeekregionSumService;
	}

	public List<NspFatallogImeiWeekregionSum> getNspFatallogImeiWeekregionSumList() {
		return nspFatallogImeiWeekregionSumList;
	}

	public void setNspFatallogImeiWeekregionSumList(
			List<NspFatallogImeiWeekregionSum> nspFatallogImeiWeekregionSumList) {
		this.nspFatallogImeiWeekregionSumList = nspFatallogImeiWeekregionSumList;
	}

	public String getSelectedId() {
		return selectedId;
	}

	public void setSelectedId(String selectedId) {
		this.selectedId = selectedId;
	}

	public String[] getSelectedIds() {
		return selectedIds;
	}

	public void setSelectedIds(String[] selectedIds) {
		this.selectedIds = selectedIds;
	}
    
	public String getMessage() {
		return message;
	}

	public void setMessage(String message) {
		this.message = message;
	}

	public List<Map> getResultList() {
		return resultList;
	}

	public void setResultList(List<Map> resultList) {
		this.resultList = resultList;
	}

	public String getQuery_fatallogImeiWeekregionSumId() {
		return this.query_fatallogImeiWeekregionSumId;
	}

	public void setQuery_fatallogImeiWeekregionSumId(
			String fatallogImeiWeekregionSumId) {
		this.query_fatallogImeiWeekregionSumId = fatallogImeiWeekregionSumId;
	}

	public String getQuery_regionCode() {
		return this.query_regionCode;
	}

	public void setQuery_regionCode(String regionCode) {
		this.query_regionCode = regionCode;
	}

	public String getQuery_logWeek() {
		return this.query_logWeek;
	}

	public void setQuery_logWeek(String logWeek) {
		this.query_logWeek = logWeek;
	}

	public String getQuery_createTime() {
		return this.query_createTime;
	}

	public void setQuery_createTime(String createTime) {
		this.query_createTime = createTime;
	}

	public String getQuery_note() {
		return this.query_note;
	}

	public void setQuery_note(String note) {
		this.query_note = note;
	}

	public String getQuery_createUser() {
		return this.query_createUser;
	}

	public void setQuery_createUser(String createUser) {
		this.query_createUser = createUser;
	}

	public String getQuery_requestType() {
		return this.query_requestType;
	}

	public void setQuery_requestType(String requestType) {
		this.query_requestType = requestType;
	}

	public String getQuery_imeiWeekCount() {
		return this.query_imeiWeekCount;
	}

	public void setQuery_imeiWeekCount(String imeiWeekCount) {
		this.query_imeiWeekCount = imeiWeekCount;
	}

	public String getQuery_imeiNewCount() {
		return this.query_imeiNewCount;
	}

	public void setQuery_imeiNewCount(String imeiNewCount) {
		this.query_imeiNewCount = imeiNewCount;
	}

	public String getQuery_imeiAllCount() {
		return this.query_imeiAllCount;
	}

	public void setQuery_imeiAllCount(String imeiAllCount) {
		this.query_imeiAllCount = imeiAllCount;
	}

	public String getQuery_logWeek_end() {
		return this.query_logWeek_end;
	}

	public void setQuery_logWeek_end(String query_logWeek_end) {
		this.query_logWeek_end = query_logWeek_end;
	}
	public String getRegionName() {
		return regionName;
	}

	public Map<String, String> getRequestTypeList() {
		return requestTypeList;
	}

	public void setRequestTypeList(Map<String, String> requestTypeList) {
		this.requestTypeList = requestTypeList;
	}

	public String getQuery_channel() {
		return query_channel;
	}

	public void setQuery_channel(String query_channel) {
		this.query_channel = query_channel;
	}

	public Map getRequestTypeMap() {
		return requestTypeMap;
	}

	public void setRequestTypeMap(Map requestTypeMap) {
		this.requestTypeMap = requestTypeMap;
	}

	public String getFlag() {
		return flag;
	}

	public void setFlag(String flag) {
		this.flag = flag;
	}

	public void setRegionName(String regionName) {
		this.regionName = regionName;
	}

	public Map<String, String> getRegionMap() {
		return regionMap;
	}

	public void setRegionMap(Map<String, String> regionMap) {
		this.regionMap = regionMap;
	}
	public Map<String, String> getChannelMap() {
		return channelMap;
	}
	public void setChannelMap(Map<String, String> channelMap) {
		this.channelMap = channelMap;
	}
	public String getChannelCode() {
		return channelCode;
	}
	
	public void setChannelCode(String channelCode) {
		this.channelCode = channelCode;
	}
}
